const sequelize = require("./db");
const Admin = require("./models/AdminModel");
const Banner = require("./models/BannerModel");
const BlogTypes = require("./models/BlogTypeModel");
const Blog = require("./models/BlogModel");
const ProjectModel = require("./models/ProjectModel");
const MessageModel = require("./models/MessageModel");
const AboutModel = require("./models/AboutModel");
const SettingModel = require("./models/SettingModel");
const md5 = require("md5");
let InitialPwd = "123456";
let InitialId = "chubai";
(async () => {
    // 文章与文章分类建立关系
    BlogTypes.hasMany(Blog, { foreignKey: "categoryId", targetKey: "id", })
    Blog.belongsTo(BlogTypes, { foreignKey: 'categoryId', targetKey: "id", as: "category" })
    // 评论与文章建立关系
    Blog.hasMany(MessageModel, { foreignKey: "blogId", targetKey: "id" })
    MessageModel.belongsTo(Blog, { foreignKey: "blogId", targetKey: "id", as: "blog" });
    //同步
    await sequelize.sync({ alter: true });
    // 管理员初始化
    const AdminDataCount = await Admin.count();
    if (!AdminDataCount) {
        const loginPwd = md5(InitialPwd);
        await Admin.create({
            loginId: InitialId,
            loginPwd,
            name: "初白"
        })
        console.log("数据初始化完毕!");
    }
    // 首页初始化
    const BannerDataCount = await Banner.count();
    if (!BannerDataCount) {
        await Banner.bulkCreate([{
            "midImg": "/static/images/bg1_mid.jpg",
            "bigImg": "/static/images/bg1_big.jpg",
            "title": "塞尔达旷野之息",
            "description": "2017年年度游戏，期待续作"
        }, {
            "midImg": "/static/images/bg2_mid.jpg",
            "bigImg": "/static/images/bg2_big.jpg",
            "title": "塞尔达四英杰",
            "description": "四英杰里面你最喜欢的又是谁呢"
        }, {
            "midImg": "/static/images/bg3_mid.jpg",
            "bigImg": "/static/images/bg3_big.jpeg",
            "title": "日本街道",
            "description": "动漫中经常出现的日本农村街道，一份独特的恬静"
        }]);
        console.log("Banner 初始化数据成功!");
    }

    // 关于页面初始化
    const AboutDataCount = await AboutModel.count();
    if (!AboutDataCount) {
        await AboutModel.create({
            url: "https://yapi.duyiedu.com/project/76/interface/api/198"
        });
        console.log("About页面 初始化数据成功!");
    }
    // 全局设置初始化
    const SettingDataCount = await SettingModel.count();
    if (!SettingDataCount) {
        await SettingModel.create({
            "avatar": "http://www.duyiedu.com/source/img/logo.png", // 博主照片
            "siteTitle": "初白的空间", // 网站标题
            "github": "https://github.com/DuYi-Edu", // 博主github主页
            "qq": "1840683922", // 博主 qq
            "qqQrCode": "http://www.duyiedu.com/source/img/%E5%B0%8F%E6%B8%A1%E5%BE%AE%E4%BF%A1%E4%BA%8C%E7%BB%B4%E7%A0%81.png", // 博主qq二维码
            "weixin": "Warmlight_Indulge", // 博主微信号
            "weixinQrCode": "http://www.duyiedu.com/source/img/%E5%85%AC%E4%BC%97%E5%8F%B7%E4%BA%8C%E7%BB%B4%E7%A0%81.png", // 博主微信二维码
            "mail": "1840683922@qq.com", // 博主邮箱
            "icp": "赣州ICP备052302066号", // 网站备案号
            "githubName": "DuYi-Edu", // 博主github名称
            "favicon": "http://mdrs.yuanjin.tech/Fs4CDlC6mwe_WXLMIiXcmSJLHO4f", // 网站图标
        });
        console.log("全局设置加载完毕内容!");
    }
    console.log("数据加载完毕..");
})();